package com.bytes32.classification.tree.dataset;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;

import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableUtils;

/*
 * Wraps the key
 */
public class ByteArrayWrapper implements Writable{

	/* the byte array */
	private byte[] wrapped;
	
	@Override
	public void readFields(DataInput arg0) throws IOException {
		this.wrapped = WritableUtils.readCompressedByteArray(arg0);
	}

	@Override
	public void write(DataOutput arg0) throws IOException {
		WritableUtils.writeCompressedByteArray(arg0, this.wrapped);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(wrapped);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ByteArrayWrapper other = (ByteArrayWrapper) obj;
		if (!Arrays.equals(wrapped, other.wrapped))
			return false;
		return true;
	}

	public void setWrapped(byte[] wrapped) {
		this.wrapped = wrapped.clone();
	}

	public byte[] getWrapped() {
		return wrapped.clone();
	}
}